package com.zxy.study.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.view.View;

import com.zxy.study.R;
import com.zxy.study.ui.widget.SignatureView;


import java.io.IOException;


/*
 *  @项目名：  study
 *  @包名：    com.zxy.study
 *  @文件名:   SignatureActivity
 *  @创建者:   zhangxy
 *  @创建时间:  2019/3/19 17:05
 *  @描述：    TODO
 */
public class SignatureActivity extends Activity {

	private SignatureView mSignatureView;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signature);

		mSignatureView = findViewById(R.id.signature_view);

		findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (ContextCompat.checkSelfPermission(SignatureActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
					writerSD();
				} else {
					ActivityCompat.requestPermissions(SignatureActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
				}

			}
		});

	}

	@Override
	public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
		//根据请求是否通过的返回码进行判断，然后进一步运行程序
		if (grantResults.length > 0 && requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
			writerSD();
		}
	}

	private void writerSD() {
		String path = Environment.getExternalStorageDirectory() + "/1.png";
		try {
			mSignatureView.save(path, true, 10);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
